package com.iiapk.gridnew;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.TextView;
import android.widget.Toast;

import com.iiapk.gridnew.domain.Article;
import com.iiapk.gridnew.fragment.WebViewFragment;
import com.iiapk.gridnew.util.TokenStore;
import com.weibo.net.Weibo;

public class WebViewPagerActivity extends FragmentActivity {
	
	private static final String CONSUMER_KEY = "173085409";
	private static final String CONSUMER_SECRET = "008e5ab868cc8e470bbee30077b773bd";
	
	private static final String QQ_CONSUMER_KEY = "801077924";
	private static final String QQ_CONSUMER_SECRET = "ba570ff433887e7315e127a990cfdc5a";
	
	private static final String picPath = null;
	public static String content = null;

	private FragmentPagerAdapter gridPagerAdapter;
	private ViewPager gridPager;
	private List<Fragment> pageviews= new ArrayList<Fragment>();
	private LayoutAnimationController controller;
	private List<Article> list;
	private TextView web_panel_header_text;
	private Article article;
	private RefreshTask refreshTask;

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.web_panel);
		list = (List<Article>)getIntent().getSerializableExtra("datasource");
		int _index = getIntent().getIntExtra("index", 0);
		web_panel_header_text = (TextView)findViewById(R.id.web_panel_header_text);
		article = list.get(_index);
		content =  article.title+"  "+article.content;
		web_panel_header_text.setText(article.title);
		
		findViewById(R.id.web_panel_backBT).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		findViewById(R.id.web_panel_header_homeBT).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getBaseContext(),GridPagerActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.zoom_enter,R.anim.hold);
			}
		});
		
		findViewById(R.id.web_panel_pre).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				int i = gridPager.getCurrentItem()-1;
				i=i<0?0:i;
				gridPager.setCurrentItem(i);
			}
		});
		findViewById(R.id.web_panel_next).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				int i = gridPager.getCurrentItem()+1;
				i=i>list.size()-1?list.size():i;
				gridPager.setCurrentItem(i);
			}
		});
		findViewById(R.id.web_panel_shoucang).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				beginRefreshTask();
			}
		});
		findViewById(R.id.web_panel_share).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				AlertDialog.Builder  builder = new AlertDialog.Builder(getLayoutInflater().getContext());
				builder.setTitle(R.string.dialog_share);
				String[] strs = getResources().getStringArray(R.array.dialog_share);
				builder.setItems(strs, new android.content.DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if(which==0){
							Weibo.getInstance().transferToToken(WebViewPagerActivity.this
								,CONSUMER_KEY,CONSUMER_SECRET,content,picPath);
						}else{
							TokenStore.get(WebViewPagerActivity.this, QQ_CONSUMER_KEY, QQ_CONSUMER_SECRET);
						}
						dialog.dismiss();
					}
				});
				builder.create().show();
			}
		});
		
		if(list!=null){
			for(Article article:list){
				pageviews.add(new WebViewFragment(article.content));
			}
		}
		
		gridPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager());
		gridPager = (ViewPager) findViewById(R.id.web_gridpager1);
		controller = AnimationUtils.loadLayoutAnimation(this,R.anim.layout_random_fade_im);
		gridPager.setLayoutAnimation(controller);
		gridPager.setAdapter(gridPagerAdapter);
		gridPager.setCurrentItem(_index);
		gridPager.setOnPageChangeListener(new OnPageChangeListener(){
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageSelected(int arg0) {
				article = list.get(arg0);
				web_panel_header_text.setText(article.title);
				content =  article.title+"  "+article.content;
			}
		});
	}

	private class FragmentPagerAdapter extends FragmentStatePagerAdapter {
		public FragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}
		@Override
		public int getCount() {
			return pageviews.size();
		}
		@Override
		public Fragment getItem(int position) {
			return pageviews.get(position);
		}
		@Override
		public Object instantiateItem(View arg0, int arg1) {
			/*if(gridPager.getChildAt(arg1)==null){
				Fragment view = (Fragment)super.instantiateItem(arg0, arg1);
				return view;
			}else
				return pageviews.get(arg1);*/
			return super.instantiateItem(arg0, arg1);
		}
		@Override
		public void destroyItem(View container, int position, Object object) {
			super.destroyItem(container, position, object);
		}
	
	}

	@Override
	protected void onPause() {
		super.onPause();
		overridePendingTransition(R.anim.hold,R.anim.zoom_out);
	}
	
	private void beginRefreshTask() {
		if (refreshTask != null) {
			refreshTask.cancel(true);
		}
		refreshTask = new RefreshTask();
		refreshTask.execute();
	}
	
	class RefreshTask extends AsyncTask<String, Void, Void> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		@Override
		protected Void doInBackground(String... params) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			Toast.makeText(WebViewPagerActivity.this,getString(
				R.string.web_panel_shoucang_msg),Toast.LENGTH_LONG).show();
		}
	}
	
}